$(function(){
	active();
	findType();
});
function findType(){
	$("#typeSelect").html("");
	var url="../../matype/query.action";
	var data=null;
	$.post(url,data,function(mes){
		$("#typeSelect").append("<option value=''>====请选择====</option>");
		$.each(mes,function(index,item){
			$("#typeSelect").append("<option value="+item.typeId+">"+item.typeName+"</option>");
			load();
		});
	},"json");
}
function exit(){
	$("#snBomBody").html("");
	$("#materInput").val("");
	$("#typeSelect").val("");
	$("#maName").val("");
	load();
	//得到iframe弹框的名字
	var index=parent.layer.getFrameIndex(window.name);
	//从父页面关闭
	parent.layer.close(index);
}
layui.use(['form', 'layedit', 'laydate','table'], function(){
	var table = layui.table
	,laydate = layui.laydate
	,form = layui.form
	,layer = layui.layer
	,layedit = layui.layedit;

	//监听复选
	table.on('checkbox(AddCom)', function(obj){
		var arr=table.cache.AddCom;
		if(obj.type=="one"){
			console.info("单选"+obj.data);

			if(obj.checked){
				$("#materInput").val($("#materInput").val()+obj.data.maId+"@");
			}else{
				var arr=$("#materInput").val().split("@");
				var it="";
				$("#materInput").val("");
				$.each(arr,function(index,item){
					if(item!=obj.data.maId && item !=null && item!='' ){
						$("#materInput").val($("#materInput").val()+item+"@");
					}
				});
			}
		}else if(obj.type=="all"){

			if(obj.checked){
				console.info("全选选中");
				$.each(arr,function(index,item){
					var state=0;
					$.each($("#materInput").val().split("@"),function(i,info){
						if(info==item.maId){
							state=1;
						}
					});
					if(state==0){
						$("#materInput").val($("#materInput").val()+item.maId+"@");
					}
				});

			}else{
				console.info("全选取消");
				$.each(arr,function(index,item){
					var arrs="";
					$.each($("#materInput").val().split("@"),function(i,info){
						if(info!=item.maId && info!='' && info!=null){
							arrs+=info+"@";
						}
					});
					$("#materInput").val(arrs);

				});
			}
		}


		$.ajaxSetup({
			async: false
		});
		var materSum="";
		$.each($(".materNumber"),function(nuI,nuItem){
			materSum+=$(nuItem).val()+","+nuItem.id+"@"
		});
		setTimeout(function(){
			Checkbox(materSum);
		},200);

		$.ajaxSetup({
			async: true
		});

	});

	//监听工具条
	table.on('tool(demo)', function(obj){
		var da = obj.data;

		if(obj.event === 'del'){
			layer.confirm('真的删除行么', function(index){
				var index = layer.load(0, {shade: false});
				var url="../../snbom/delete.action";
				var data={"bomId":da.bomId}
				$.post(url,data,function(mes){
					layer.close(index);
					if(mes==1){
						obj.del();
						layer.close(index);
						layer.msg("删除成功");
					}else{
						layer.msg("删除失败");
					}
				},"json");

			});
		}

	});
	var $ = layui.$, active = {
			reload: function(){
				var typeId = $('#typeSelect');
				var maName = $('#maName');
				//执行重载
				table.reload('AddCom', {
					page: {
						curr: 1 //重新从第 1 页开始
					}
				,where: {
					typeId: typeId.val(),
					maName: maName.val(),
				}
				});
			}
	,getCheckData:function(){
		console.info(table.cache);

		var sum="";
		$.each($("#snBomBody tr"),function(index,item){
			console.info("材料id:"+$(item).children(":first").html());
			console.info("采购数量:"+$(item).children(":first").next().next().children(":first").val());
			sum+=$(item).children(":first").html()+","+$(item).children(":first").next().next().children(":first").val()+"@";
		});
		if(sum.length==0){
			layer.msg("请先添加材料");
		}else{
			var index = layer.load(0, {shade: false});
			var comId=getParameter("comId");
			var url="../../snbom/addOrUpdate.action";
			var data={"sum":sum,"comId":comId,"bomCommont":$("#bomCommont").html()};
			$.post(url,data,function(mes){
				layer.close(index);
				if(mes==1){
					parent.layer.msg('保存成功', {
						icon: 1
					});
					exit();
				}else if(mes==0){
					layer.msg('保存失败', {
						icon: 2
					});
				}	
			},"json");
		}

	}
	};



	$('#selectCom').on('click', function(){
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	
	$('#selectButton').on('click', function(){
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	

	$('.demoTable #insert').on('click', function(){
		layer.open({
			type: 1	 //此处以iframe举例
			,title:false
			,closeBtn :0
			,area:['700px','360px']
		,offset: 'auto'
			,content:$('#insertBom')
			,success: function(layero){

				//执行重载
				table.reload('AddCom', {
					page: {
						curr: 1 //重新从第 1 页开始
					}
				});
				//调用默认选中的方法
				//findSuccess();
			}
		});
	});


});
function Checkbox(materSum){
	$("#snBomBody").html("");

	$.each($("#materInput").val().split("@"),function(index,item){
		if(item!=""){
			var urls="../../materials/findById.action";
			var datas={"maId":item};
			$.post(urls,datas,function(info){
				console.info(info.maName);
				$("#snBomBody").append("<tr>"
						+"<td class='purchasemaid'>"+info.maId+"</td>"
						+"<td>"+info.maName+"</td>"
						+"<td><input type='text' value='0' id='number"+info.maId+"' class='materNumber' onkeyup='findkeyup(\""+info.maId+"\",\""+info.maMoney+"\")' onkeydown='findkeyup(\""+info.maId+"\",\""+info.maMoney+"\")'/></td>"
						+"<td>"+info.maUnit+"</td>"
						+"<td><button class='layui-btn layui-btn-danger layui-btn-xs' id='id"+info.maId+"' onclick='findDelete(\""+info.maId+"\",\""+info.maName+"\")'>删除</button></td>"
						+"</tr>");
				$.each(materSum.split("@"),function(is,infos){
					var shuzu=infos.split(",");
					$("#"+shuzu[1]).val(shuzu[0]);
				});
				load();
			},"json");

		}
	});
}
function findDelete(id,bomName){
	$("#id"+id).parent().parent().remove();
	
	var arr=$("#materInput").val().split("@");
	$("#materInput").val("");
	$.each(arr,function(index,item){
		if(item!=id && item !=null && item!='' ){
			$("#materInput").val($("#materInput").val()+item+"@");
		}
	});
	$.each($(".layui-table-col-special"),function(index,item){
		var name=$(item).next().next().next().children().html();
		if(name!=undefined && name!=''){
			if(name==bomName){
				console.info($(item).children().children(":first"));
				var check=$(item).children().children(":first").get(0);
				$(check).attr({"checked":false});
				load();
			}
		};
	});
}
function findkeyup(a,b){
	if($("#number"+a).val()=='' || isNaN($("#number"+a).val())){
		$("#number"+a).val(0);
	}else if($("#number"+a).val().length>7){
		$("#number"+a).val($("#number"+a).val().substring(0,$("#number"+a).val().length-1));
	}
}
var comId;
function active(){
	comId=getParameter("comId");
	layui.use('table', function(){
		var table = layui.table;
		var $ = layui.$, active = {
				reload: function(){
					//var proId = $('#proId');
					//执行重载
					table.reload('snBom', {
						page: {
							curr: 1 //重新从第 1 页开始
						}
					,where: {
						comId: comId,
					}
					});
				}
		};

		active['reload'] ? active['reload'].call(this) : '';
	});
}

function getParameter(param)
{
	var query = window.location.search;
	var iLen = param.length;
	var iStart = query.indexOf(param);
	if (iStart == -1)
		return "";
	iStart += iLen + 1;
	var iEnd = query.indexOf("&", iStart);
	if (iEnd == -1)
		return query.substring(iStart);

	return query.substring(iStart, iEnd);
}
function load() {
	var form = layui.form
	form.render();
}